package com.cfpl.admin_vegetable_service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cfpl.admin_vegetable_service.VO.SearchResult;
import com.cfpl.admin_vegetable_service.mapper.WarehouseMapper;
import com.cfpl.admin_vegetable_service.service.WarehouseService;
import com.cfpl.project_common.context.BaseContext;
import com.cfpl.project_common.pojo.VegetableEntry;
import com.cfpl.project_common.pojo.Warehouse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
* @author 14720
* @description 针对表【warehouse(仓库管理表)】的数据库操作Service实现
* @createDate 2023-11-02 10:16:29
*/
@Service
public class WarehouseServiceImpl extends ServiceImpl<WarehouseMapper, Warehouse>
    implements WarehouseService {

    @Autowired
    private WarehouseMapper warehouseMapper;

    /**
     * 分页查询仓库数据
     * @param currentPage
     * @param pageSize
     * @param condition
     * @return
     */
    @Override
    public Page<Warehouse> queryPage(int currentPage, int pageSize, String condition) {
        LambdaQueryWrapper<Warehouse> queryWrapper = new LambdaQueryWrapper<>();
        if (condition != null) {
            queryWrapper.like(Warehouse::getProductName,condition).or()
                    .eq(Warehouse::getWarehouseAddress,condition);
        }

        return warehouseMapper.selectPage(new Page<>(currentPage,pageSize),queryWrapper);
    }

    /**
     * 添加仓库数据
     * @param warehouse
     */
    @Override
    public void addWarehouse(Warehouse warehouse) {
//        定义当前时间
        Date date = new Date();
//        设定要转换的格式 20231111150820 20231111150020 20231111120120 20231111112120
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
        String dateString = formatter.format(date);
//        手动获取管理员id
        warehouse.setAdminId(BaseContext.getCurrentUserId());
//        进仓时间
        warehouse.setWarehouseInDate(date);
        warehouse.setIdentifyCode(dateString);
        warehouseMapper.insert(warehouse);
    }

    /**
     * 查询追溯信息
     */
    @Override
    public SearchResult queryTraceabilityInfo(String identifyCode) {
        return warehouseMapper.queryTraceabilityInfo(identifyCode);
    }

}




